OpenCV: Mask operations on matrices |
您所在的位置:网站首页 › opencv pow › OpenCV: Mask operations on matrices |
Prev Tutorial: How to scan images, lookup tables and time measurement with OpenCV Next Tutorial: Operations with images Original author Bern谩t G谩bor Compatibility OpenCV >= 3.0Mask operations on matrices are quite simple. The idea is that we recalculate each pixel's value in an image according to a mask matrix (also known as kernel). This mask holds values that will adjust how much influence neighboring pixels (and the current pixel) have on the new pixel value. From a mathematical point of view we make a weighted average, with our specified values. Our test caseLet's consider the issue of an image contrast enhancement method. Basically we want to apply for every pixel of the image the following formula: \[I(i,j) = 5*I(i,j) - [ I(i-1,j) + I(i+1,j) + I(i,j-1) + I(i,j+1)]\] \[\iff I(i,j)*M, \text{where } M = \bordermatrix{ _i\backslash ^j & -1 & 0 & +1 \cr -1 & 0 & -1 & 0 \cr 0 & -1 & 5 & -1 \cr +1 & 0 & -1 & 0 \cr }\] The first notation is by using a formula, while the second is a compacted version of the first by using a mask. You use the mask by putting the center of the mask matrix (in the upper case noted by the zero-zero index) on the pixel you want to calculate and sum up the pixel values multiplied with the overlapped matrix values. It's the same thing, however in case of large matrices the latter notation is a lot easier to look over. Code C++You can download this source code from here or look in the OpenCV source code libraries sample directory at samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp. #include #include #include #include using namespace std; using namespace cv; static void help(char* progName) { cout |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |